package com.lovo.ssc.config;


import com.lovo.util.MQUtil;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQConfig {
    //spring封装rabiitmq的代码 RabbitTemplate
//注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }
/***********************   队列   ********************/
    //队列
    @Bean
    public Queue maintainQueue (){
        return new Queue(MQUtil.maintainQueue );
    }

    //未上架商品队列
    @Bean
    public Queue pendingCommodityQueue(){
        return new Queue(MQUtil.pendingCommodityQueue);
    }

    //退货订单队列
    @Bean
    public Queue returnOrderQueue(){
        return new Queue(MQUtil.returnOrderQueue);
    }

    @Bean
    public Queue orderQueue1(){
        return new Queue(MQUtil.ORDERQUEUE1);
    }

    @Bean
    public Queue orderQueue2(){
        return new Queue(MQUtil.ORDERQUEUE2);
    }
    //发送预存款队列
    @Bean
    public Queue moneyQueue(){
        return new Queue(MQUtil.moneyQueue);
    }


    /***********************   交换机   ********************/
    //交换机
    @Bean
    public DirectExchange registerExchange (){
        return new DirectExchange(MQUtil.registerExchange );
    }

    //未上架商品交换机
    @Bean
    public FanoutExchange pendingCommodityExchange(){
        return new FanoutExchange(MQUtil.pendingCommodityFanoutExchange);
    }

    //退货订单交换机
    @Bean
    public FanoutExchange returnOrderFanoutExchange(){
        return new FanoutExchange(MQUtil.returnOrderFanoutExchange);
    }

    @Bean
    public FanoutExchange orderFanoutExchange(){
        return new FanoutExchange(MQUtil.ORDERFANOUTEXCHANGE);
    }


    //预存款交换机
    @Bean
    public DirectExchange moneyDirectExchange (){
        return new DirectExchange(MQUtil.moneyDirectExchange);
    }


    /***********************   绑定队列和交换机   ********************/
     //绑定交换机和队列
    @Bean
    public Binding directExchangeMovieSellrQueue(Queue maintainQueue,DirectExchange registerExchange){
     return BindingBuilder.bind(maintainQueue).to(registerExchange).with(MQUtil.maintainKey );
    }

    //绑定未上架商品交换机和未上架商品队列
    @Bean
    public Binding pendingCommodityExchangeAndpendingCommodityQueue(Queue pendingCommodityQueue,FanoutExchange pendingCommodityExchange){
        return BindingBuilder.bind(pendingCommodityQueue).to(pendingCommodityExchange);
    }

    //绑定退货订单交换机和退货订单队列
    @Bean
    public Binding returnOrderFanoutExchangeAndreturnOrderQueue(Queue returnOrderQueue,FanoutExchange returnOrderFanoutExchange){
        return BindingBuilder.bind(returnOrderQueue).to(returnOrderFanoutExchange);
    }



    //绑定预存款交换机和队列
    @Bean
    public Binding moneyQueueToMoneyDirectExchange(Queue moneyQueue, DirectExchange moneyDirectExchange) {
        return BindingBuilder.bind(moneyQueue).to(moneyDirectExchange).with(MQUtil.moneyKey);
    }

    @Bean
    public Binding orderQueue1ToOrderFanoutExchange(Queue orderQueue1,FanoutExchange orderFanoutExchange){
        return BindingBuilder.bind(orderQueue1).to(orderFanoutExchange);
    }

    @Bean
    public Binding orderQueue2ToOrderFanoutExchange(Queue orderQueue2,FanoutExchange orderFanoutExchange){
        return BindingBuilder.bind(orderQueue2).to(orderFanoutExchange);
    }
}
